package cn.tedu.myweb.controller;


import cn.tedu.myweb.entity.Depository;
import cn.tedu.myweb.exception.ServiceException;
import cn.tedu.myweb.mapper.UserMapper;
import cn.tedu.myweb.model.User;
import cn.tedu.myweb.service.IUserService;
import cn.tedu.myweb.vo.RegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
// lombok提供的一个记录日志用的注解
// 一旦在类上添加@Slf4j,这个类的方法中就可以使用log对象记录日志
@Slf4j
public class SystemController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    private IUserService userService;
    @PostMapping("/register")
    public String register(
            // RegisterVo参数前添加@Validated表示开启服务器端验证功能
            // 当控制器方法运行之前,SpringValidation框架会按RegisterVo
            // 中编写的规则进行验证
            @Validated RegisterVo registerVo,
            // 这个参数必须紧随RegisterVo之后
            // result中包含RegisterVo的验证结果
            BindingResult result){
        //利用日志对象,将接收到的信息输出到控制台
        log.debug("接收到用户信息:{}",registerVo);
        if(result.hasErrors()){
            String msg=result.getFieldError().
                                        getDefaultMessage();
            // 返回错误信息
            return msg;
        }
        try {
            userService.register(registerVo);
            return "OK";
        }catch (ServiceException e){
            log.error("注册失败",e);
            return e.getMessage();
        }
    }
    //定义图片路径
    @Value("${dirPath}")
    private String dirPath;
    //查询个人信息
    @RequestMapping("/user/selectById")
    public User selectById(@AuthenticationPrincipal UserDetails user,
            HttpSession session){

        return userMapper.selectAllById(userMapper.findUserByUsername(user.getUsername()).getId());
    }

    //修改个人信息
    @RequestMapping("user/upload")
    public void uploadById(User user,  MultipartFile picFile) throws IOException {
        System.out.println("user = " + user + "picFile = " + picFile);
        String fileName = picFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;


        SimpleDateFormat format = new SimpleDateFormat("/yyyy/MM/dd/");

        String datePath = format.format(new Date());

        File dirFile = new File(dirPath+datePath);

        if (!dirFile.exists()){
            dirFile.mkdirs();
        }

        String filePath = dirPath+datePath+fileName;

        picFile.transferTo(new File(filePath));

        user.setPicUrl(datePath+fileName);
        userMapper.updateById(user);

    }
}










